from peewee import *

db = MySQLDatabase('nemtocr', host='localhost', port=3800, user='root', password='!@mtg23YWZ^&')


class University(Model):
    id = PrimaryKeyField()
    year = IntegerField()
    code = CharField(max_length=32)
    name = CharField(max_length=64)
    logo = CharField(max_length=256)
    province_code = IntegerField()
    city_code = IntegerField()
    province = CharField(max_length=16)
    city = CharField(max_length=16)
    type_name = CharField(max_length=32)
    level_name = CharField(max_length=32)
    nature_name = IntegerField()
    other_type = IntegerField()
    site = CharField(max_length=256)
    enrol_site = CharField(max_length=256)
    university_rank = IntegerField()
    university_rank_text = CharField(max_length=16)
    telephone = CharField(max_length=64)
    accept_add = CharField(max_length=64)
    enter_rule = CharField(max_length=128)
    change_difficult_level = CharField(max_length=32)
    jicha_2 = FloatField()
    jicha_3 = FloatField()
    jicha_4 = FloatField()
    jicha_5 = FloatField()
    jicha_6 = FloatField()
    dual_class_name = CharField(max_length=32)
    dual_class_A = IntegerField()
    dual_class_B = IntegerField()
    dual_class_subject = IntegerField()
    f985 = IntegerField()
    f211 = IntegerField()
    is_deleted = IntegerField()

    class Meta:
        database = db  # This model uses the "spider" database.
        table_name = 'university_2'  # 院校信息表


    # 插入单条数据(返回新增id)
    def toInsert(self, row):
        try:
            return University.insert(row).execute()
        except Exception as e:
            return ''

    # 批量插入数据
    def toInsertBatch(self, rows):
        with db.atomic():
            University.insert_many(rows).execute()


    # 根据id，获取单条记录
    def getOne(self, id):
        try:
            return University.select().where(University.id == id).get()
        except Exception as e:
            return ''


    # 根据院校代码code，获取单条记录
    def getOneByCode(self, code):
        try:
            return University.select().where((University.code == code) & (University.year == 2023)).order_by(University.year.desc()).dicts().get()
        except Exception as e:
            return ''


    # 根据年份查询列表
    def getList(self, year):
        try:
            return University.select(University.id, University.accept_add).where(University.year == year).dicts().execute()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdate(self, row):
        try:
            return University.update(
                nature_name=row['nature_name'], other_type=row['other_type']
            ).where((University.year == row['year']) & (University.code == row['code'])).execute()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdateName(self, row):
        try:
            return University.update(
                accept_add=row['accept_add']).where(University.id == row['id']).execute()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdateBase(self, row):
        try:
            return University.update(
                name=row['name'], level_name=row['level_name'], university_rank=row['university_rank'],
                university_rank_text=row['university_rank_text'], type_name=row['type_name'],
                nature_name=row['nature_name'], change_difficult_level=row['change_difficult_level'],
            ).where((University.year == row['year']) & (University.code == row['code'])).execute()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdateBase2(self, row):
        try:
            return University.update(
                change_difficult_level=row['change_difficult_level'],
            ).where(University.code == row['code']).execute()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdateRule(self, row):
        try:
            return University.update(
                level_name=row['level_name'], accept_add=row['accept_add'], enter_rule=row['enter_rule'],
                jicha_2=row['jicha_2'], jicha_3=row['jicha_3'],
                jicha_4=row['jicha_4'], jicha_5=row['jicha_5'], jicha_6=row['jicha_6'],
            ).where(University.code == row['code']).execute()
        except Exception as e:
            return ''


if __name__ == '__main__':
    unversity = University()
    try:
        university_row = unversity.getOneByCode('10001')
        print(university_row)
    except Exception as e:
        print("except:", e)
